package com.cysion.otbox.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.cysion.otbox.Box;
import com.cysion.otbox.R;
import com.dovar.dtoast.DToast;

public class MyToast {

    private static long lastToastTime = 0L;

    public static void toast(Context aContext, String msg) {
        if (aContext == null || msg == null) {
            return;
        }
        View view = LayoutInflater.from(aContext).inflate(R.layout.toast_normal, null);
        if (view == null) {
            return;
        }
        TextView textView = view.findViewById(R.id.tv_text);
        if (textView == null) {
            return;
        }
        textView.setText(msg);
        if(System.currentTimeMillis()-lastToastTime<60){
            return;
        }
        lastToastTime = System.currentTimeMillis();
        Box.INSTANCE.cfg();
        DToast.make(aContext)
                .setView(view)
                .setGravity(Gravity.CENTER, 0, (int) (Box.INSTANCE.density() * 50))
                .show();
    }

    public static void toast(Context aContext, String msg, boolean isLong) {
        if (aContext == null || msg == null) {
            return;
        }
        View view = LayoutInflater.from(aContext).inflate(R.layout.toast_normal, null);
        if (view == null) {
            return;
        }
        TextView textView = view.findViewById(R.id.tv_text);
        if (textView == null) {
            return;
        }
        textView.setText(msg);
        if(System.currentTimeMillis()-lastToastTime<60){
            return;
        }
        lastToastTime = System.currentTimeMillis();
        DToast.make(aContext)
                .setView(view)
                .setDuration(DToast.DURATION_LONG)
                .setGravity(Gravity.CENTER, 0, (int) (Box.INSTANCE.density() * 50))
                .show();
    }

    public static void toast(Context aContext, int msgId) {
        String msg = Box.INSTANCE.str(msgId);
        toast(aContext, msg);
    }
}
